Example: .NET Script

The dataset will be modified to include the added fields and the values as specified in the script.

Example 1: Generate random output values

Initial dataset:

C# configuration:

Output fields:

Name

Type

NewDoubleField

double

NewIntegerField

int

Variables:

Name

Type

myRandom

Random

Script:

if (FirstExecute)

{

    // Must call the constructor of the Random type

    myRandom = new Random();

}

 

NewDoubleField = myRandom.NextDouble() * 20; // Doubles 0 to 20

NewIntegerField = myRandom.Next() % 20 + 1; // Integers 1 to 20

Resulting dataset:

VB.NET configuration:

Output fields:

Name

Type

NewDoubleField

double

NewIntegerField

int

Variables:

Name

Type

myRandom

Random

Script:

If FirstExecute

    ' Must call the constructor of the Random type

    myRandom = new Random()

End If

 

NewDoubleField = myRandom.NextDouble() * 20 ' Doubles 0 to 20

NewIntegerField = myRandom.Next() mod 20 + 1 ' Integers 1 to 20

Resulting dataset:

 

Example 2: Get values from a text file

Initial dataset:

Text file "MyFile"

C# configuration:

Output fields:

Name

Type

FieldStringOutput

string

Variables:

Name

Type

rowIndex

int

streamReader

System.IO.StreamReader

Script:

if (FirstExecute)

{

    // Open a custom text file you have saved on your drive

    streamReader = System.IO.File.OpenText("c:/temp/MyFile.txt");

 

    // Initialize the row index to 0 and increase after every script execute.

    rowIndex = 0;

}

 

int totalRows = 4;

 

// Set the string output value to the line in your text file for only the

// first 4 rows, otherwise set it to 'null'.

 

if (rowIndex < totalRows)

{

    try

    {

        FieldStringOutput = streamReader.ReadLine();

    }

    catch( Exception e )

    {

        streamReader.Close();

    }

}

 

else

{    

    streamReader.Close(); // Close the reader if it has not been closed yet

    FieldStringOutput = "DEFAULT";

}

++rowIndex;

Resulting dataset:

VB.NET configuration:

Output fields:

Name

Type

FieldStringOutput

string

Variables:

Name

Type

rowIndex

Integer

streamReader

System.IO.StreamReader

Script:

If FirstExecute

    ' Open a custom text file you have saved on your drive

    streamReader = System.IO.File.OpenText("c:/temp/MyFile.txt")

 

    ' Initialize the row index to 0 and increase after every script execute.

    rowIndex = 0

End If

 

Dim totalRows As Integer = 4

 

' Set the string output value to the line in your text file for only the

' first 4 rows, otherwise set it to 'null'.

 

If rowIndex < totalRows

 

    Try    

        FieldStringOutput = streamReader.ReadLine()    

    Catch e As Exception

        streamReader.Close()

    End Try

 

Else    

 

    streamReader.Close() ' Close the reader if it has not been closed yet

    FieldStringOutput = "DEFAULT"

 

End If

rowIndex = rowIndex + 1

 

Resulting dataset:

Example 3: Save only unique values

Initial dataset:

C# configuration:

Output fields:

Name

Type

DoubleOutputField

double

Variables:

Name

Type

listPreviousValues

List<double>

Script:

if (FirstExecute)

{

    // Initialize the System.Collections.Generic.List type

    // and keep all values from previous script executes in it.

 

    listPreviousValues = new List<double>();

}

 

// Set DoubleOutputField to null by default

DoubleOutputField = null;

 

if (Field1 != null)

{

    // Only add unique output values that have not been added before

    // as well as unique timestamps

 

    if (!listPreviousValues.Contains(Field1.Value))

    {

        listPreviousValues.Add(Field1.Value);

 

        // Set to inputDouble 's value if it is the first time

        // encountering the value.

        DoubleOutputField = Field1;

    }        

}

Resulting dataset:

VB.NET configuration:

Output fields:

Name

Type

DoubleOutputField

double

Variables:

Name

Type

listPreviousValues

(Of Double)

Script:

If FirstExecute

 

    ' Initialize the System.Collections.Generic.List type

    ' and keep all values from previous script executes in it.

    listPreviousValues = new List(Of Double)

 

End If

 

' Set DoubleOutputField to Nothing by default

DoubleOutputField = Nothing

 

If Field1.HasValue()

    ' Only add unique output values that have not been added before

    ' as well as unique timestamps

    If (Not(listPreviousValues.Contains(Field1.Value)))

    

        listPreviousValues.Add(Field1.Value)

 

        ' Set to inputDouble 's value if it is the first time

        ' encountering the value.

        DoubleOutputField = Field1

    End If

End If

Resulting dataset:


Related topics:

  

CSense 2023- Last updated: June 24,2025